/*
 * @Author: zxc
 * @Date: 2020-08-15 15:27:08
 * @LastEditors: zxc
 * @Description: 选择排序法
 */
import { Compare, defaultCompare, swap } from "../utils";

export const selectionSort = (array, compareFn = defaultCompare) => {
  const { length } = array;
  let indexMin;
  for (let i = 0; i < length - 1; i++) {
    indexMin = i;
    for (let j = i; j < length; j++) {
      if (compareFn(array[indexMin], array[j]) === Compare.BIGGER_THAN) {
        indexMin = j;
      }
    }
    if (i !== indexMin) {
      swap(array, i, indexMin);
    }
  }
  return array;
};
